﻿using QRCoder;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HttpRequest
{
    public partial class QRCodeForm : Form
    {
        public QRCodeForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 生成二维码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCreateQR_Click(object sender, EventArgs e)
        {
            try
            {
                var path = Application.StartupPath + $"{DateTime.Now.ToString("yyyyMMddHHmmssffff")}.png";
                GenerateQRCode("http://www.baidu.com", path);

                picQR.Image = Image.FromFile(path);
                picQR.SizeMode = PictureBoxSizeMode.AutoSize;
            }
            catch (Exception ex)
            {
                MessageBox.Show("二维码生成失败。" + ex.Message);
            }
        }

        public void GenerateQRCode(string text, string filePath)
        {
            QRCodeGenerator qrGenerator = new QRCodeGenerator();
            QRCodeData qrCodeData = qrGenerator.CreateQrCode(text, QRCodeGenerator.ECCLevel.Q);
            QRCode qrCode = new QRCode(qrCodeData);

            using (Bitmap qrImage = qrCode.GetGraphic(20, Color.Black, Color.White, true))
            {
                qrImage.Save(filePath, ImageFormat.Png);
            }
        }

        private void btnOpenFile_Click(object sender, EventArgs e)
        {

        }
    }
}
